package cn.minimelon.api.service.data.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.noear.solon.Solon;

import javax.sql.DataSource;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.SQLException;

@Slf4j
public class SQLRunnerUtils {
    public static void execScript(String sql) throws SQLException {
        DataSource dataSource = Solon.context().getBean(DataSource.class);
        try (Connection conn = dataSource.getConnection()) {
            StringReader reader = new StringReader(sql);
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setDelimiter(";;");
            runner.runScript(reader);
        } catch (SQLException ex) {
            log.error("execScript error", ex);
            throw ex;
        }
    }
}
